package com.app.hmst.main;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.hmst.model.HomestayInfoVO;
import com.app.hmst.model.HouseInfoVO;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class SubMenuLevel1Adapter extends BaseAdapter{
	private List<String> temp;
	private Activity act;
	private ViewHolderForFirst viewForFirst;
	private SlidingMenu _sm;
	private ListView _lvPrevious;
	private HomestayInfoVO _infoVo;
	public SubMenuLevel1Adapter(Activity _act, List<String> _temp,SlidingMenu sm,ListView lvPrevious, HomestayInfoVO _infoVo){
		this.act = _act;
		this.temp = _temp;
		this._sm = sm;
		this._lvPrevious = lvPrevious;
		this._infoVo = _infoVo;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return temp.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return temp.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (position == 0) {
			viewForFirst = new ViewHolderForFirst();
			LayoutInflater inflater = (LayoutInflater) act.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			convertView = inflater.inflate(R.layout.each_sub_level_one,null);
			viewForFirst.subListMenuLevelTwo = (ListView) convertView.findViewById(R.id.subListMenuLevelTwo);
			viewForFirst.each_level_two_menu_list = (RelativeLayout) convertView.findViewById(R.id.each_level_two_menu_list);
			viewForFirst.subListMenuLevelTwo.setVisibility(View.GONE);
			viewForFirst.menu_arrow = (ImageView) convertView.findViewById(R.id.imageView2);
			viewForFirst.homeStayTitle = (TextView) convertView.findViewById(R.id.textView1);
			viewForFirst.menu_icon = (ImageView) convertView.findViewById(R.id.imageView1);
			viewForFirst.homeStayTitle.setText(_infoVo.name);
			viewForFirst.menu_arrow.setImageDrawable(act.getResources().getDrawable(R.drawable.arrow_left));
			viewForFirst.each_level_two_menu_list.setOnClickListener(new ClickListToShowSubList2Listener(viewForFirst.subListMenuLevelTwo, 0, viewForFirst.menu_arrow));
			
			if("C".equals(_infoVo.status)){
				viewForFirst.menu_icon.setImageDrawable(act.getResources().getDrawable(R.drawable.marker_icon_selected));
			}else{
				viewForFirst.menu_icon.setImageDrawable(act.getResources().getDrawable(R.drawable.marker_icon));
			}
			convertView.setTag(viewForFirst);

		}
		if (position == 1) {
			viewForFirst = new ViewHolderForFirst();
			LayoutInflater inflater = (LayoutInflater) act.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			convertView = inflater.inflate(R.layout.each_sub_add_home,null);
			viewForFirst.add_new_homestay = (RelativeLayout) convertView.findViewById(R.id.add_new_homestay);

			viewForFirst.add_new_homestay.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
				}
			});
			convertView.setTag(viewForFirst);

		}
		return convertView;
	}
	public class ViewHolderForFirst {
		private RelativeLayout add_new_homestay,each_level_two_menu_list;
		private ListView subListMenuLevelTwo;
		private ImageView menu_arrow,menu_icon;
		private TextView homeStayTitle;
	}
	public class ClickListToShowSubList2Listener implements OnClickListener{

		private ListView tgListView;
		private int _ste = 0;
		private ImageView imgV;
		public ClickListToShowSubList2Listener(ListView targetListView,int state,ImageView imgV){
			this.tgListView = targetListView;
			this._ste =state;
			this.imgV = imgV;
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			if(_ste==0){
				int height = 100;
				
				imgV.setImageDrawable(act.getResources().getDrawable(R.drawable.arrow_down));
				ViewGroup.LayoutParams params = _lvPrevious.getLayoutParams();
			     params.height = height*_infoVo.houses().size() ;
				_lvPrevious.setLayoutParams(params);
			    _lvPrevious.requestLayout();
			    
			    _lvPrevious.setDividerHeight(0);
			    tgListView.setVisibility(View.VISIBLE);
				_ste=1;
				new InitAndLoadSubList(tgListView).execute();
			}else{
				tgListView.setVisibility(View.GONE);
				imgV.setImageDrawable(act.getResources().getDrawable(R.drawable.arrow_left));
				_ste=0;
			}			
			
		}
		
	}
	private void switchFragment(Fragment fragment) {
		if (act == null)
			return;

		if (act instanceof HomeStayMainActivity) {
			HomeStayMainActivity fca = (HomeStayMainActivity) act;
			fca.switchContent(fragment);
		}
	}
	public class InitAndLoadSubList extends AsyncTask<String, Void, List<HouseInfoVO>> implements OnCancelListener{
		ProgressHUD mProgressHUD;
		private ListView _viewList;
		public InitAndLoadSubList(ListView listView){
			this._viewList = listView;
		}
		
    	@Override
    	protected void onPreExecute() {
        	mProgressHUD = ProgressHUD.show(act,"Loading Content...", true,true,this);
    		super.onPreExecute();
    	}
		@Override
		protected List<HouseInfoVO> doInBackground(String... params) {
			// TODO Auto-generated method stub
			
			return _infoVo.houses();
		}
		
		@Override
		protected void onPostExecute(List<HouseInfoVO> result) {
			super.onPostExecute(result);
			if (result != null) {
				int height = 50;
				ViewGroup.LayoutParams params = _viewList.getLayoutParams();
			    //params.height = height*result.size();
				params.height = height*_infoVo.houses().size();
				_viewList.setLayoutParams(params);
			    _viewList.requestLayout();
			    _viewList.setDividerHeight(0);
				mProgressHUD.dismiss();
				SubMenuLevel2Adapter subLevel1 = new SubMenuLevel2Adapter(act, result,_sm,_viewList,_infoVo);
				_viewList.setAdapter(subLevel1);
				
			} else {
				mProgressHUD.dismiss();
			}
			
		}
		@Override
		public void onCancel(DialogInterface dialog) {
			// TODO Auto-generated method stub
			mProgressHUD.dismiss();
		}
	}
}
